<template>
    <div class="login-container">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
            <div class="welcome-login">
                <div class="title">欢迎登录</div>
                <div class="title-en">LOGIN</div>
            </div>
            <el-form-item prop="account">
                <el-input
                    ref="account"
                    v-model="loginForm.account"
                    placeholder="请输入用户名"
                    name="account"
                    type="text"
                    tabindex="1"
                    auto-complete="on"
                    prefix-icon="el-icon-user"
                />
            </el-form-item>

            <el-form-item prop="password">
                <el-input
                    ref="password"
                    v-model="loginForm.password"
                    placeholder="请输入密码"
                    name="password"
                    tabindex="2"
                    auto-complete="on"
                    @keyup.enter.native="handleLogin"
                    show-password
                    prefix-icon="el-icon-unlock"
                    class="self-input"
                />
            </el-form-item>

            <el-button :loading="loading" type="primary" class="el-button-submit" @click.native.prevent="handleLogin">登录</el-button>
        </el-form>
    </div>
</template>

<script>
import { loginApi } from '@/api/login';
import { lookUserById } from '@/api/user';
export default {
    name: 'Login',
    data() {
        const validateUsername = (rule, value, callback) => {
            if (!value) {
                callback(new Error('请输入用户名'));
            } else {
                callback();
            }
        };
        const validatePassword = (rule, value, callback) => {
            if (value.length < 6) {
                callback(new Error('请输入密码'));
            } else {
                callback();
            }
        };
        return {
            loginForm: {
                account: 'admin',
                password: 'admin'
            },
            loginRules: {
                account: [{ required: true, trigger: 'blur', validator: validateUsername }]
                // 此处做密码校验，如果无密码校验注释，如果需要可添加对应正则
                // password: [
                //   { required: true, trigger: 'blur', validator: validatePassword }
                // ]
            },
            loading: false,
            redirect: undefined
        };
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect;
            },
            immediate: true
        }
    },
    methods: {
        handleLogin() {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    //验证成功后请求登录接口
                    loginApi(this.loginForm).then((res) => {
                        if (res.code == 200) {
                            this.$message.success('登录成功');
                            localStorage.setItem('ms_username', res.data);
                            // 跳转到首页还是其他页面;
                            lookUserById({ token: res.data }).then((res) => {
                                localStorage.setItem('userInfo', JSON.stringify(res.data));
                                this.$router.push({ path: '/' });
                            });
                        } else {
                            this.$message.info(res.msg);
                        }
                    });
                } else {
                    return false;
                }
            });
        }
    }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
        color: $cursor;
    }
}

/* reset element-ui css */
.login-container {
    .el-input {
        display: inline-block;
        height: 47px;
        width: 85%;
        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            color: $light_gray;
            height: 47px;
            caret-color: $cursor;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px $bg inset !important;
                -webkit-text-fill-color: $cursor !important;
            }
        }
    }

    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
    }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
    min-height: 100%;
    width: 100%;
    background: url(../assets/img/login_bg.png);
    overflow: hidden;
    background-size: cover;

    .login-form {
        width: 349px;
        height: 422px;
        background: rgba(255, 255, 255, 0.9);
        border-radius: 12px;
        border: 1px solid #ffffff;
        backdrop-filter: blur(1px);
        position: relative;
        // width: 520px;
        max-width: 100%;
        // padding: 160px 35px 0;
        margin: 0 auto;
        overflow: hidden;
        margin-top: 75px;
        padding: 64px 48px 0;
        .el-form-item {
            width: 100%;
            height: 42px;
            background: #ffffff;
            border-radius: 5px;
            border: 1px solid #2f7df1;
            ::v-deep .el-input {
                vertical-align: top;
                .el-form-item__error {
                    padding: 0;
                }
                .el-input__inner {
                    color: #242a37;
                    height: 42px;
                    line-height: 42px;
                    caret-color: #242a37;
                    padding-left: 40px;
                }
            }
        }
    }
    .svg-container {
        padding: 0 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
    }
    .show-pwd {
        position: absolute;
        right: 10px;
        top: 1px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }
    .el-button-submit {
        width: 100%;
        height: 43px;
        background: #2d4fa2;
        border-radius: 5px;
        border-color: #2d4fa2;
        margin-top: 20px;
    }
}
.welcome-login {
    text-align: center;
    margin-bottom: 28px;
    .title {
        color: #353a42;
        font-size: 20px;
    }
    .title-en {
        color: #989daa;
        font-size: 14px;
    }
}
.self-input ::v-deep .el-input__suffix {
    right: -23px;
}
</style>
